{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "88c74797-8c99-4485-abde-13f30069e371",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import DataFrame\n",
    "from datetime import datetime\n",
    "# from xlwt import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "735f64b4-8202-4025-8fdf-7ff0dd00bc8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "R_D = np.loadtxt(\"../datann/mat_drug_se.txt\")\n",
    "\n",
    "drug_name = []\n",
    "with open(\"../datann/drugname.txt\") as f:\n",
    "    for name in f:\n",
    "        drug_name.append(name)      # len(drug_name) = 763\n",
    "\n",
    "\n",
    "dis_name = []\n",
    "with open(\"../datann/se.txt\") as f:\n",
    "    for name in f:\n",
    "        dis_name.append(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0dd8345f-23d7-4f51-b508-6fceca9fdd84",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "train_id = np.loadtxt(\"../train_data.txt\")\n",
    "test_id = np.loadtxt(\"../test_data.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "975ee159-3e40-4f77-b518-abc6c915b462",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "B = R_D / 1 \n",
    "for i in range(train_id.shape[0]):\n",
    "    B[int(train_id[i][0])][int(train_id[i][1])] = -1\n",
    "test_out = np.loadtxt(\"../test_out.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "496d5e01-b9ed-4094-b82c-77ac67e1b8d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "R = np.zeros(shape=(R_D.shape[0], R_D.shape[1])) \n",
    "\n",
    "for i in range(test_id.shape[0]):\n",
    "    R[int(test_id[i][0])][int(test_id[i][1])] = test_out[i][1]\n",
    "\n",
    "R=R\n",
    "print(R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "12b50e81-235c-4a3e-ba03-23a2e1b70608",
   "metadata": {},
   "outputs": [],
   "source": [
    "from xlwt import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "526f6747-47a7-4935-a04c-00c907284f77",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "all_score=[]                      \n",
    "score=np.sort(-R)   \n",
    "for i in range(708):\n",
    "    for j in range(30):\n",
    "        all_score.append(-score[i][j])\n",
    "\n",
    "w = Workbook()\n",
    "ws = w.add_sheet('Hey, Dude')\n",
    "i = 0\n",
    "for fmt in all_score:\n",
    "    print(fmt)\n",
    "    ws.write(i, 0, fmt)\n",
    "    i += 1\n",
    "w.save('data/table_score.xls')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e7b605f6-364a-48ba-b78f-618c71d6de2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "w = Workbook()\n",
    "ws = w.add_sheet('Hey, Dude')\n",
    "i = 0\n",
    "for name in drug_name:\n",
    "    ws.write(i, 0, name)\n",
    "    i += 1\n",
    "w.save('data/drug_name.xls')\n",
    "\n",
    "\n",
    "def arg(R):\n",
    "    old_id = np.argsort(-R)  \n",
    "    # print(old_id)\n",
    "    dis_name_30 = []\n",
    "    for i in range(30):\n",
    "        name = dis_name[old_id[i]]\n",
    "        dis_name_30.append(name)\n",
    "    return dis_name_30\n",
    "\n",
    "dis_name_30 = []\n",
    "for i in range(708):       \n",
    "    R1_dis_name = arg(R[i])\n",
    "    for j in range(30):\n",
    "        name = R1_dis_name[j]\n",
    "        dis_name_30.append(name)\n",
    "\n",
    "w = Workbook()\n",
    "ws = w.add_sheet('Hey, Dude')\n",
    "i = 0\n",
    "for fmt in dis_name_30:\n",
    "    ws.write(i, 0, fmt)\n",
    "    i += 1\n",
    "w.save('data/SideEffect_name.xls')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1f39d510-e0b3-4166-a5d1-f37981729277",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlrd\n",
    "import xlwt\n",
    "\n",
    "Score = xlrd.open_workbook('data/table_score.xls')\n",
    "Drug_name = xlrd.open_workbook('data/drug_name.xls')\n",
    "Disease_name = xlrd.open_workbook('data/SideEffect_name.xls')\n",
    "\n",
    "score = Score.sheet_by_index(0)\n",
    "drug_name = Drug_name.sheet_by_index(0)\n",
    "disease_name = Disease_name.sheet_by_index(0)\n",
    "\n",
    "disease_rows = disease_name.nrows\n",
    "disease_cols = disease_name.ncols\n",
    "drug_rows = drug_name.nrows\n",
    "drug_cols = drug_name.ncols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "834e5b17-d0b3-4a6d-99e7-77c8cfd01a01",
   "metadata": {},
   "outputs": [],
   "source": [
    "Table_drug30 = xlwt.Workbook()\n",
    "table_drug30 = Table_drug30.add_sheet('Sheet1')\n",
    "j = 0\n",
    "for i in range(0, drug_rows):\n",
    "\tfor num in range(30):\n",
    "\t\ttable_drug30.write(j,0,drug_name.cell(i,0).value)  \n",
    "\t\tj = j + 1\n",
    "Table_drug30.save('data/drug_name30.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0a1311f9-c8e3-45d5-a2f5-8850d62e061b",
   "metadata": {},
   "outputs": [],
   "source": [
    "Table_rank = xlwt.Workbook()\n",
    "table_rank = Table_rank.add_sheet('Sheet2')\n",
    "for i in range(0, drug_rows):\n",
    "\tfor j in range(1, 31):\n",
    "\t\ttable_rank.write(30*i+j-1, 0, j)\n",
    "Table_rank.save('data/rank_30.xls')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c6e7df0f-3323-46c8-804d-7d606d90add4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Rank = xlrd.open_workbook('data/rank_30.xls')\n",
    "rank = Rank.sheet_by_index(0)\n",
    "Drug_name30 = xlrd.open_workbook('data/drug_name30.xls')\n",
    "drug_name30 = Drug_name30.sheet_by_index(0)\n",
    "\n",
    "Table_drug_disease_rank_score = xlwt.Workbook()\n",
    "table_drug_disease_rank_score = Table_drug_disease_rank_score.add_sheet('Sheet2')\n",
    "for i in range(0, disease_rows):\n",
    "\ttable_drug_disease_rank_score.write(i, 0, drug_name30.cell(i, 0).value)\n",
    "\ttable_drug_disease_rank_score.write(i, 1, disease_name.cell(i, 0).value)\n",
    "\ttable_drug_disease_rank_score.write(i, 2, rank.cell(i, 0).value)\n",
    "\ttable_drug_disease_rank_score.write(i, 3, score.cell(i, 0).value)\n",
    "\n",
    "Table_drug_disease_rank_score.save('data/drug_se_rank_score.xls')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc-showcode": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
